import { watch } from 'vue';
import { useGradeStore } from '@/store/modules/member';
import { useUserStore } from '@/store/modules/user';

let initialized = false;

export function initGradeWhenTokenReady() {
  const userStore = useUserStore();
  const gradeStore = useGradeStore();

  watch(
    () => userStore.token,
    (newToken) => {
      if (newToken && !initialized) {
        initialized = true;
        gradeStore.fetchGradeList();
      }
    },
    { immediate: true },
  );
}
